package com.zahb.qadx.ui.view.captcha

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.view.animation.Animation
import android.view.animation.CycleInterpolator
import android.view.animation.RotateAnimation
import android.widget.FrameLayout
import androidx.appcompat.app.AlertDialog
import com.luozm.captcha.Captcha
import com.zahb.qadx.R

/**
 * Created by Zdw on 2021/06/08 14:21
 */

class CaptchaDialog @JvmOverloads constructor(
    context: Context,
    themeResId: Int = R.style.TransparentDialog
) : AlertDialog(context, themeResId) {

    private val drawableIds =
        arrayOf(R.drawable.captcha_1, R.drawable.captcha_2, R.drawable.captcha_3)
    var listener: Captcha.CaptchaListener? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val contentView = View.inflate(context, R.layout.dialog_captcha, null)
        contentView.findViewById<View>(R.id.iv_close).setOnClickListener { dismiss() }

        //刷新按钮添加背景
        val refresh = contentView.findViewById<View>(R.id.refresh)
        val refreshParent = refresh.parent as FrameLayout
        val layoutParams = refresh.layoutParams
        refreshParent.removeView(refresh)
        val refreshContainer = FrameLayout(context)
        refreshContainer.setBackgroundColor(Color.LTGRAY)
        refreshContainer.addView(refresh, FrameLayout.LayoutParams(-1, -1))
        refreshParent.addView(refreshContainer, layoutParams)

        val captcha = contentView.findViewById<CustomCaptcha>(R.id.captcha)
        captcha.setBitmap(drawableIds[(drawableIds.indices).random()])
        captcha.setCaptchaListener(object : Captcha.CaptchaListener {
            override fun onAccess(time: Long): String? {
                contentView.findViewById<View>(R.id.seekbar).isEnabled = false
                contentView.postDelayed({
                    dismiss()
                }, 200L)
                return listener?.onAccess(time)
            }

            override fun onFailed(failCount: Int): String {
                var failed = listener?.onFailed(failCount)
                if (failed == null) failed = "验证失败"
                val animation = RotateAnimation(
                    -1f, 1f,
                    Animation.RELATIVE_TO_SELF, 0.5f,
                    Animation.RELATIVE_TO_SELF, 0.5f
                )
                animation.interpolator = CycleInterpolator(3f)
                animation.duration = 200L
                animation.setAnimationListener(object : Animation.AnimationListener {
                    override fun onAnimationStart(animation: Animation?) {

                    }

                    override fun onAnimationEnd(animation: Animation?) {
                        captcha.reset(true)
                    }

                    override fun onAnimationRepeat(animation: Animation?) {

                    }
                })
                contentView.startAnimation(animation)
                return failed
            }

            override fun onMaxFailed(): String? {
                refresh.visibility = View.GONE
                return listener?.onMaxFailed()
            }

        })
        setContentView(contentView)
    }

    companion object {
        @JvmStatic
        fun showCaptchaDialog(context: Context, listener: Captcha.CaptchaListener) {
            val dialog = CaptchaDialog(context)
            dialog.setCanceledOnTouchOutside(false)
            dialog.listener = listener
            dialog.show()
        }
    }
}